package com.wsm.kotlintest.common.net

import android.util.Log
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Created by 王家宝 on 2018/7/19.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */
class RetrofitClient {
    companion object {
        private const val BASE_URL = "https://www.reddit.com/"
        val getInstance by lazy {
            create()
        }
        private fun create(): API {
            val logger = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
                Log.i("API", it)
            })
            logger.level = HttpLoggingInterceptor.Level.BODY

            val client = OkHttpClient.Builder()
                    .addInterceptor(logger)
                    .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                    .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .build()

            return Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(
                            GsonBuilder()
                                    .serializeNulls()
                                    .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
                                    .setPrettyPrinting()
                                    .create()
                    ))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
                    .create(API::class.java)
        }


    }

}